library(htmltools)
APP_URL <- "https://pchen-aitutor.hf.space" # ← your Gradio app URL
USER_ID <- "anonymous_user"
browsable(tagList(
# --- styles (fab, bubble, modal) ---
tags$style(HTML("
#tutor-fab{position:fixed;right:20px;bottom:20px;z-index:99999;padding:12px 14px;border-radius:999px;border:1px solid #ddd;background:#fff;box-shadow:0 8px 24px rgba(0,0,0,.15);cursor:pointer;font-weight:600}
#tutor-fab:hover{background:#f6f8ff}
#tutor-sel-btn{position:absolute;display:none;z-index:100000;padding:6px 10px;font-size:12px;border:1px solid #e5e7eb;border-radius:10px;background:#111827;color:#f9fafb;box-shadow:0 8px 24px rgba(0,0,0,.2);cursor:pointer;user-select:none}
#tutor-modal-overlay{display:none;position:fixed;z-index:99998;inset:0;background:rgba(0,0,0,.35)}
#tutor-modal{position:absolute;top:5%;left:50%;transform:translateX(-50%);width:min(980px,95vw);height:min(720px,90vh);background:#fff;border-radius:10px;box-shadow:0 10px 30px rgba(0,0,0,.25);overflow:hidden}
#tutor-modal-header{display:flex;justify-content:space-between;align-items:center;padding:10px 14px;background:#f5f5f7;border-bottom:1px solid #e5e5ea;font-weight:600}
#tutor-close-btn{cursor:pointer;border:none;background:transparent;font-size:18px}
#tutor-iframe{width:100%;height:calc(100% - 18px);border:0}
")),
# modal + iframe
tags$div(id="tutor-modal-overlay",
tags$div(id="tutor-modal",
tags$div(id="tutor-modal-header","AI Calculus Tutor", tags$button(id="tutor-close-btn","✕")),
tags$iframe(id="tutor-iframe", src="")
)
),
# floating button + selection bubble
tags$button(id="tutor-fab","Ask Tutor"),
tags$button(id="tutor-sel-btn","Ask Tutor"),
# script
tags$script(HTML(sprintf("
(function(){
const APP_URL = '%s';
const USER_ID = '%s';
function sel(){ try { return (window.getSelection?window.getSelection().toString():'').trim(); } catch(e){ return ''; } }
function openInline(q, action){
const p = new URLSearchParams({ user_id: USER_ID, q: q||'', action: action||'prefill' });
const frame = document.getElementById('tutor-iframe');
frame.src = APP_URL + '/?' + p.toString(); // Prefill handled by app from URL
document.getElementById('tutor-modal-overlay').style.display='block';
}
function openTab(q, action){
const p = new URLSearchParams({ user_id: USER_ID, q: q||'', action: action||'prefill' });
window.open(APP_URL + '/?' + p.toString(), 'ai_tutor_tab', 'noopener');
}
// floating button: prefill by default; Alt=send; Ctrl/Cmd=new tab
document.getElementById('tutor-fab').addEventListener('click', function(ev){
const q = sel();
const act = ev.altKey ? 'send' : 'prefill';
if (ev.ctrlKey || ev.metaKey) openTab(q, act); else openInline(q, act);
});
// selection bubble near highlighted text
const b = document.getElementById('tutor-sel-btn');
let last='';
function place(x,y){ b.style.left=(x+10)+'px'; b.style.top=(y+10)+'px'; }
function show(){ b.style.display='block'; } function hide(){ b.style.display='none'; }
document.addEventListener('mouseup', e => setTimeout(() => {
const t = sel(); if (t && t.length>=2 && t.length<=800){ last=t; place(e.pageX,e.pageY); show(); } else hide();
}, 20));
window.addEventListener('scroll', hide, {passive:true}); window.addEventListener('resize', hide);
// bubble click: prefill by default; Alt=send; Ctrl/Cmd=new tab
b.addEventListener('click', function(ev){
if (!last) return;
const act = ev.altKey ? 'send' : 'prefill';
if (ev.ctrlKey || ev.metaKey) openTab(last, act); else openInline(last, act);
hide();
});
// close modal
const overlay = document.getElementById('tutor-modal-overlay');
document.getElementById('tutor-close-btn').addEventListener('click', () => overlay.style.display='none');
overlay.addEventListener('click', e => { if (e.target===overlay) overlay.style.display='none'; });
})();
", APP_URL, USER_ID)))
))By the end of this 90-minute session, students will be able to:
R skills:
function()Bean & Brew Café wants to optimize its operations using mathematical modeling. The owner needs to understand:
In R, functions can be defined using several approaches:
Method 1: Vectorised operations in R
By vectorised operations we mean that the input \(x\) is a sequence of real numbers (a vector). Applying an expression to a vector produces a new vector \(y\): each value in \(x\) is mapped to exactly one value in \(y\).
R code for a function defined using vectorised operations is as follows:
# x <- seq(from, to, by = step)
# y <- expression_in_x
## Example
x <- seq(0,10,0.1)
y = 0.5*x + 2
plot(x,y,type = "l", main = "Linear Function Y = 0.5 X + 2", xlab = "X", ylab = "Y")Method 2: Using function() keyword
R code using function() is like follows:
For \[y = 0.5x + 2\]
The following R code defines the function and plot its graph.
# f <- function(x) {
# return(expression_in_x)
# }
## Example
f <- function(x) {
return(0.5*x+2)
}
x <- seq(0,10,0.1)
y <- f(x)
plot(x, y, type = "l", main = "Linear Function Y = 0.5 X + 2", xlab = "X", ylab = "Y")Vectorised operations are often simple, but they are less flexible and are not applicable in some situations.
The price is a piecewise linear function.
\[C(d) = \begin{cases} 10 & \text{if } 0 \leq d \leq 5 \\ 10 + 2(d-5) & \text{if } 5 < d \leq 15 \\ 10 + 20 + 1.5(d-15) & \text{if } d > 15 \end{cases}\]
In this case we can only use function() to define the
price function in R.
C<-function(d){
if ((d>0)&( d <= 5)) {price<-10}
if ((d>5)&( d <= 15)) {price<- 10 + 2*(d-5)}
if (d>15) {price <- 10 + 20 + 1.5*(d-15)}
return(price)
}
d<- seq(1,30)
P<-d*0
# We cannot do P <- C(d) because the if-conditions in C(d) assume that d is a single number (not a vector).
# Instead, we compute the price for each distance value one-by-one.
for (i in 1:length(d)) {
P[i] = C(d[i])
}
plot(d,P,type = "l", main = "Price of Delivery",
xlab = "Distance (km)", ylab = "Price")Use the R keyword function() to define a function that
takes another function \(f\) as an
input and plots its graph over a chosen domain. Then test your plotting
function by graphing each of the following functions (choose a suitable
domain for each):
A linear function that passes through the point \((x_0,y_0)= (2,3)\) and has slope \(b=1.5\).
\(f(x) = \sin(2x)\)
\(f(x)=\frac{1}{1+e^{-x}}\)
\(f(x) = \frac{x^3+1}{5+x^2}\)
The code snippets above can be used as templates to complete this exercise.
Use the R keyword function() to define the following
function and draw its graph over the domain \(x\in[0,10]\) in the R environment below (or
in your own local RStudio). You can copy the sample code above and
modify it to define the function.
\[f(x) = \begin{cases} 2x & \text{if } 0 \leq x \leq 5 \\ 10 + 4(x-5) & \text{if } 5 < x \\ \end{cases}\]
Plot a function using the function as input
In R, a function can take many types of inputs, including another
function. Here is an example of a plotting function that takes a
function f and a vector x as inputs, and then
plots \(f(x)\) over the domain
specified by x.
Plot_function <- function(f, x) {
y <- f(x)
plot(x, y, type = "l")
}
### Note: Plot_function can be used to plot any function of $x$. See the following two examples.
## Define a function Line_point_slope: it passes through (x0, y0) with slope b
x0 = 3
y0 = 4
b = 2
Line_point_slope <- function(x) {
y <- y0 + b*(x - x0)
return(y)
}
## Define a function Polynomial3: a cubic (degree 3) polynomial
Polynomial3 <- function(x) {
y <- x^3 + 2*x^2 - 4*x - 1
return(y)
}
x <- seq(0, 10, 0.1)
Plot_function(Line_point_slope, x)InnovateTech Corp is optimizing its operations across multiple dimensions:
The CEO asks: “How can we visualize these multivariable relationships in R to make better strategic decisions?”
A function \[f : \mathbb{R}^n \to \mathbb{R}\] is called a multivariable function if it maps multiple inputs \((x_1,x_2,...,x_n)\) to a real number \(f(x_1,x_2,...,x_n)\).
Example of a linear function \[f(x,y) = x + 2y - 4\] This function takes two inputs \(x\) and \(y\) and returns a value that depends linearly on \(x\) and \(y\).
Example of a quadratic function \[f(x,y) =x^2+y^2\] \[f(x,y) = x^2+3xy+y^2\] These functions take two inputs \(x\) and \(y\) and return a quadratic expression (a sum of squares, possibly with a cross term).
In plotting a univariable function \(y = f(x)\):
For a multivariable function \(z=f(x,y)\) we use the same idea:
\[f(x,y) = x+ 2y -4\] A function of two variables can be visualised as a surface over in a 3D coordinate system, where \((x,y)\) are the input variables and \(z\) is the value of the output variable. For the linear function the surface will be a plane.
As in the case of univariate functions, we create sequences for the input variables \(x\) and \(y\). For each pair \((x,y)\) we calculate the function value \(z=f(x,y)\). The resulting \(z\)-values are then connected to form the surface.
Surface plot
x <- seq(-10, 10, length.out = 30)
y <- seq(-10, 10, length.out = 30)
f <- function(x, y) { x + 2*y - 4 }
z <- outer(x, y, f)
#op <- par(bg = "white")
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")Contour Plot
A commonly used visualisation for a multivariable function is the contour plot. The link below shows terrain contour lines for the Snowy Mountains in Google Maps:
Rice fields on hills create contour lines (level lines) in the landscape. Since rice needs water, the ridges around the fields must be kept even.
The rows of seats in a stadium are similar to the contour lines of a surface.
Along a contour line the height is constant, so contour lines are also called level lines. In economics, contour lines of a production function are called isoquants. The direction perpendicular to a contour line is the direction of steepest ascent (or descent).
Heat map
Heat maps are often used in weather forecasts to visualise temperature over an area (a function of longitude and latitude). The colour represents the function value (temperature).
For the linear function above:
Gradient field
A heat map with contour lines shows where the surface is steep or flat. At any point, the direction perpendicular to the contour line is the direction of steepest ascent (and the opposite direction is steepest descent). A gradient field visualises this by drawing an arrow at each point that points in the direction of steepest ascent; longer arrows indicate a steeper slope.
plot(1, type = "n", xlim = range(x), ylim = range(y),
xlab = "Labour (L)", ylab = "Capital (K)",
main = "Gradient Field of Q(L,K)")
# Define partial derivatives (gradient components)
dQ_dx <- function(x, y) {
1 # ∂Q/∂L
}
dQ_dy <- function(x, y) {
2 # ∂Q/∂K
}
for(l in x) {
for(k in y) {
arrows(l, k,
l + dQ_dx(l, k)/8,
k + dQ_dy(l, k)/8,
length = 0.04, col = "blue")
}
}
On a plane, the direction of maximum ascent is the same everywhere;
therefore, the arrows are identical at each point.
Interactive surface
Interactive surface plots allow you to rotate and zoom the 3D surface so you can view it from different perspectives.
Slice
A slice (cross-section) is a 2D plot obtained by fixing one input variable at a constant value, so the function depends on only one variable. In economics, slices are often used to study how the output changes with one input while holding the other input constant (ceteris paribus).
# --- Slice Plot ---
plot(x, f(x, y = 0), type = "l", col = "blue", lwd = 2,
xlab = "Labour (L)", ylab = "Function value", main = "Slice of Plane Function (y = 0, 2, 4)")
lines(x, f(x, y = 2), col = "red", lwd = 2)
lines(x, f(x, y = 4), col = "green", lwd = 2)
legend("bottomright", legend = c("y = 0", "y = 2", "y = 4"), col = c("blue", "red", "green"), lwd = 2)\[Q(L,K)=20L^{0.7}K^{0.3}\]
Surface plot
L <- seq(0, 10, length.out = 30)
K <- seq(0, 10, length.out = 30)
f <- function(L, K) { 20*L^0.7*K^0.3 }
z <- outer(L, K, f)
#op <- par(bg = "white")
persp(L, K, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")Contour Plot
Heat Map
Gradient Field
# Define partial derivatives (gradient components)
dQ_dL <- function(L, K) {
20 * 0.7 * L^(-0.3) * K^0.3 # ∂Q/∂L
}
dQ_dK <- function(L, K) {
20 * 0.3 * L^0.7 * K^(-0.7) # ∂Q/∂K
}
plot(1, type = "n", xlim = range(L), ylim = range(K),
xlab = "Labour (L)", ylab = "Capital (K)",
main = "Gradient Field of Q(L,K)")
# Skip L = 0 or K = 0 to avoid infinite derivatives in the gradient.
for (l in L[L > 0]) {
for (k in K[K > 0]) {
arrows(l, k,
l + dQ_dL(l, k)/100,
k + dQ_dK(l, k)/100,
length = 0.04, col = "blue")
}
}
In a gradient field plot, the direction of an arrow points to the
direction of ascent, and the length of the arrow indicates the steepness
of that ascent. The longer the arrow, the steeper the ascent.
Interactive surface
Slice
# --- Slice Plot ---
plot(L, f(L, K = 5), type = "l", col = "blue", lwd = 2,
xlab = "Labour (L)", ylab = "Output", main = "Slice of Production Function (K = 5)")
lines(L, f(L, K = 2), col = "red", lwd = 2)
lines(L, f(L, K = 1), col = "green", lwd = 2)
legend("bottomright", legend = c("K = 5", "K = 2","K = 1"), col = c("blue", "red","green"), lwd = 2)Visualise the following functions using surface plot, contour plot, heat map, slice, and 3D interactive surface plot.
A plane that passes through \((x_0,y_0) = (2,3)\) with slopes \(a=1.5\) in the \(x\)-direction and \(b=3\) in the \(y\)-direction. (Hint: the plane function is \(f(x,y) = f(x_0,y_0) + a(x-x_0) + b(y-y_0)\).)
\(f(x,y) = x^2 + y^2\)
using the following R Environment or your own local RStudio.
Finding where supply equals demand:
This requires us to solve this system of two linear equations at the same time.
TechProd Manufacturing produces three products: smartphones (S), tablets (T), and laptops (L). The company needs to determine optimal production levels based on resource constraints.
Resource Requirements per Unit:
Labour hours: 2S + 3T + 4L = 1000 hours available
Materials (kg): 1S + 2T + 3L = 600 kg available
Machine time: 3S + 1T + 2L = 800 hours available
This real-world problem requires us to solve a system of three linear equations at the same time.
How do we proceed? First recall how we solve a single linear equation:
\[ax=b \quad (a\neq 0)\]
We can divide both sides by \(a\) (equivalently, multiply both sides by \(a^{-1}\)):
\[a^{-1}ax = a^{-1}b\]
This leads to the solution:
\[x = a^{-1}b\]
For \(a=3\) and \(b=2\), the R solution is as follows:
## [,1]
## [1,] 0.6666667
Can we follow the same procedure to solve a system of linear equations? The answer is yes. Here is how.
Example 2.4: The Manufacturing Company
Step 1: Move all unknowns to the left-hand side and the known constants to the right-hand side. Arrange the unknown variables in a consistent order (here: \(S\) first, \(T\) second, and \(L\) third).
\[2S + 3T + 4L = 1000\] \[1S + 2T + 3L = 600\] \[3S + 1T + 2L = 800\] Step 1.1: Write the unknowns as a vector:
Put all unknowns into one sequence as a vector \(X=\left( \begin{array}{c} S\\ T\\ L\\ \end{array} \right)\)
and the known constants on the right-hand side as a vector \(b=\left( \begin{array}{c} 1000\\ 600\\ 800\\ \end{array} \right)\)
Step 1.2: Put the coefficients of the system into a matrix:
\(A=\left( \begin{array}{ccc} 2&3&4\\ 1&2&3\\ 3&1&2\\ \end{array} \right)\)
Note that for a system of 3 equations we have 3 unknowns, so the vector \(X\) has 3 elements: \[\begin{array}{c} S\\ T\\ L\\ \end{array}\])$; and the right-hand-side constants form the vector \(b=\left( \begin{array}{c} 1000\\ 600\\ 800\\ \end{array} \right)\);
The coefficient matrix is a \(3\times 3\) square matrix. Its entries must match the chosen order of variables (this is why the order of variables matters).
\[\underbrace{\left( \begin{array}{rrr} 2 & 3 & 4\\ 1 & 2 & 3\\ 3 & 1 & 2\\ \end{array} \right)}_{A} \underbrace{\left( \begin{array}{c} S\\ T\\ L\\ \end{array} \right)}_{X} = \underbrace{\left( \begin{array}{r} 1000\\ 600\\ 800\\ \end{array} \right)}_{b} \]
Step 2: Multiply both sides by the inverse of the coefficient matrix \(A\) (assuming \(A\) is invertible).
\[A^{-1}AX = A^{-1}b\] Since \(A^{-1}A=I\), this leads to:
\[X = A^{-1}b\]
R solution is as follows:
## [,1]
## [1,] 2.000000e+02
## [2,] 2.000000e+02
## [3,] 5.684342e-14
The solution is \(S=200\), \(T=200\), and \(L=0\).
Exercise 2.3: The Market Equilibrium (Revisited)
After moving the unknowns to the left-hand side, we can write the system in matrix form:
\[\underbrace{\left( \begin{array}{rr} 1 & -2 \\ 1 & 1 \\ \end{array} \right)}_{A} \underbrace{\left( \begin{array}{c} Q\\ P\\ \end{array} \right)}_{X} = \underbrace{\left( \begin{array}{r} -100\\ 300\\ \end{array} \right)}_{b} \]
R solution
## [,1]
## [1,] 166.6667
## [2,] 133.3333
This is exactly the same solution we have obtained by substitution method in the previous section.
Solve the market equilibrium problem
Solve the problem above using the R environment below (or your own local RStudio).
A system of \(m\) linear equations in \(n\) unknowns has the form: \[\begin{cases} a_{11}x_1 + a_{12}x_2 + \cdots + a_{1n}x_n = b_1 \\ a_{21}x_1 + a_{22}x_2 + \cdots + a_{2n}x_n = b_2 \\ \vdots \\ a_{m1}x_1 + a_{m2}x_2 + \cdots + a_{mn}x_n = b_m \end{cases}\]
This system can be written compactly as: \(\mathbf{Ax} = \mathbf{b}\)
Where: - \(\mathbf{A} = \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{bmatrix}\) ,\(\hspace{0.5cm}\) \(\mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix}\) ,\(\hspace{0.5cm}\) \(\mathbf{b} = \begin{bmatrix} b_1 \\ b_2 \\ \vdots \\ b_m \end{bmatrix}\)
\[\mathbf{x} = \mathbf{A}^{-1}\mathbf{b}\]
We have been using vector operations in R since the previous section. The example above—solving systems of linear equations using matrix notation—demonstrates the usefulness of vector and matrix notation. Now we formalise these operations and highlight some of the additional benefits of working with vectors and matrices.
Properties: - Associative: \((\mathbf{AB})\mathbf{C} = \mathbf{A}(\mathbf{BC})\) - Distributive: \(\mathbf{A}(\mathbf{B} + \mathbf{C}) = \mathbf{AB} + \mathbf{AC}\) - Not commutative: \(\mathbf{AB} \neq \mathbf{BA}\) (in general)
Two vectors are parallel to each other: \(\mathbf{x} = \lambda \mathbf{y}\) or \[\begin{bmatrix} x_1 \\ x_2 \end{bmatrix}=\lambda \begin{bmatrix} y_1 \\ y_2 \end{bmatrix}\].
Two vectors are perpendicular to each other: \(\mathbf{x}'\mathbf{y} = 0\) or \[\begin{bmatrix} x_1 & x_2 \end{bmatrix} \begin{bmatrix} y_1 \\ y_2 \end{bmatrix}=0\].
A vector with \(n\) elements is called an \(n\)-vector. A matrix with \(m\) rows and \(n\) columns is called an \(m\times n\) matrix. Verify the following statements using the matrix operation rules above.
For a square matrix \(\mathbf{A}\), the inverse matrix \(\mathbf{A}^{-1}\) satisfies: \[\mathbf{A}\mathbf{A}^{-1} = \mathbf{A}^{-1}\mathbf{A} = \mathbf{I}\]
where \(\mathbf{I}\) is the identity matrix.
\(\mathbf{A}^{-1}\) exists if and only if: 1. \(\mathbf{A}\) is square (\(n \times n\)) 2. \(\det(\mathbf{A}) \neq 0\) (A is non-singular)
Geometric explanation (in 2D): \(\det(A)=0\) means the two equations are linearly dependent, so the lines are either parallel or identical.
Using pencil and paper for \(2\times 2\) matrices: \[\mathbf{A}^{-1} = \frac{1}{\det(\mathbf{A})} \begin{bmatrix} d & -b \\ -c & a \end{bmatrix}\] where \(\mathbf{A} = \begin{bmatrix} a & b \\ c & d \end{bmatrix}\) and \(\det(\mathbf{A}) = ad - bc\)
For \(\mathbf{A} = \begin{bmatrix} 2 & 3 \\ 3 & 5 \end{bmatrix}\) we have \(\det(\mathbf{A})=1\), so the formula gives \(\mathbf{A}^{-1} = \begin{bmatrix} 5 & -3 \\ -3 & 2 \end{bmatrix}\).
Using R
### assign the matrix
A <- matrix(c(2, 3, 3, 5), 2, 2, byrow = TRUE)
### calculate the determinant
det(A)## [1] 1
## [,1] [,2]
## [1,] 5 -3
## [2,] -3 2
Computing an inverse matrix by hand is practical only for very small matrices such as \(2\times 2\) matrices. For larger matrices, it is almost always done using a computer.
Calculate the determinant and the inverse of \(\mathbf{A} = \begin{bmatrix} 2 & 3 \\ 3 & 5 \end{bmatrix}\) using the following R Environment or your local RStudio.
Solving a nonlinear system of equations is generally more complicated than solving a linear system. Geometrically, a nonlinear equation in two variables defines a curve in the \((x,y)\)-plane. A system of two nonlinear equations corresponds to two curves; their intersection points (if any) are the solutions. The interactive graph below lets you explore the solutions of a system of two nonlinear equations in two variables.
In business and economics, quadratic functions are of particular interest. Consider the one-variable quadratic: \[f(x) = ax^2\]
Question: When is this function always positive (except at \(x=0\))? (For example, when does the business activity guarantee a positive result?)
Answer: When \(a > 0\)
This is the one-variable case of positive definiteness: the parabola opens upward and has a minimum at \(x=0\).
Extending this to the multivariate case, consider \[f(\mathbf{x}) = \mathbf{x}^\top A \, \mathbf{x}= \sum_{i=1}^n\sum_{j=1}^n a_{ij}x_i x_j\] where \(\mathbf{x}\) is a vector and \(A\) is a square \(n\times n\) matrix. If this function is always positive for all \(\mathbf{x}\neq \mathbf{0}\), then \(A\) is called positive definite.
In physics, the potential energy stored in a stretched spring is always positive (unless there is no displacement):
\[ E = \frac{1}{2}\mathbf{x}^\top K\mathbf{x}\] where \(K\) is a stiffness matrix. For the system to make physical sense, energy must be positive \(\to\) \(K\) must be positive definite.
Global Portfolio Management is analyzing investment risk using quadratic forms. The firm’s risk model for a two-asset portfolio is:
\[\text{Risk} = w_1^2\sigma_1^2 + w_2^2\sigma_2^2 + 2w_1w_2\sigma_{12}\]
Where: + \(w_1, w_2\) are portfolio weights
\(\sigma_1^2, \sigma_2^2\) are asset variances
\(\sigma_{12}\) is the covariance between assets
This can be written as a quadratic form: \[\text{Risk} = \mathbf{w}^T\mathbf{\Sigma}\mathbf{w}\] with \(\mathbf{w}=\left( \begin{array}{c} w_1\\ w_2\\ \end{array} \right)\) and \(\mathbf{\Sigma} =\left(\begin{array}{cc} \sigma_1^2& \sigma_{12}\\ \sigma_{12}& \sigma_2^2\\ \end{array} \right)\)
The risk manager asks: “How can we determine if our covariance matrix ensures the risk is always positive (positive definite), and how do we visualize this?”
A symmetric matrix \(\mathbf{A}\) is positive definite if: \[\mathbf{x}^T\mathbf{A}\mathbf{x} > 0 \text{ for all } \mathbf{x} \neq \mathbf{0}\]
Equivalent conditions:
All eigenvalues of \(\mathbf{A}\) are positive
All leading principal minors are positive
\(\mathbf{A} = \mathbf{L}\mathbf{L}^T\) for some invertible matrix \(\mathbf{L}\) (Cholesky decomposition)
## [,1] [,2]
## [1,] 2 1
## [2,] 1 3
## eigen() decomposition
## $values
## [1] 3.618034 1.381966
##
## $vectors
## [,1] [,2]
## [1,] 0.5257311 -0.8506508
## [2,] 0.8506508 0.5257311
# Create a grid over [-10, 10]
x <- seq(-10, 10, length.out = 100)
y <- seq(-10, 10, length.out = 100)
z <- outer(x, y, function(x, y) {
mapply(function(x, y) {
X <- c(x, y)
t(X) %*% A %*% X
}, x, y)
})
filled.contour(x, y, z)Interactive surface
A symmetric matrix \(\mathbf{A}\) is negative definite if: \[\mathbf{x}^T\mathbf{A}\mathbf{x} < 0 \text{ for all } \mathbf{x} \neq \mathbf{0}\]
All eigenvalues of \(\mathbf{A}\) are negative
## [,1] [,2]
## [1,] -2 1
## [2,] 1 -3
## eigen() decomposition
## $values
## [1] -1.381966 -3.618034
##
## $vectors
## [,1] [,2]
## [1,] -0.8506508 -0.5257311
## [2,] -0.5257311 0.8506508
# Create a grid over [-10, 10]
x <- seq(-10, 10, length.out = 100)
y <- seq(-10, 10, length.out = 100)
z <- outer(x, y, function(x, y) {
mapply(function(x, y) {
X <- c(x, y)
t(X) %*% A %*% X
}, x, y)
})
filled.contour(x, y, z)Interactive surface
For \(A =\begin{bmatrix} -4 & -1 \\ -1 & -6 \end{bmatrix}\), create 3D plots of the quadratic function \(f(\mathbf{x}) = \mathbf{x}^\top A\mathbf{x}\) over the domain \(x_1\in[-1,1]\), \(x_2\in[-1,1]\) using the R environment below (or your own local RStudio).
This section provides review and application questions designed to assess understanding at five different levels. Each level builds upon the previous one, fostering deeper comprehension and the ability to apply concepts in increasingly complex situations.
https://pchen-exercisesfeedbackselect.hf.space
This comprehensive Section 2 covers the essential mathematical foundations for business analysis:
Preparation for Section 3: These tools provide the foundation for understanding derivatives, which measure rates of change and enable optimization in business contexts.
Key Takeaways: - Mathematics provides powerful tools for business decision-making - Multiple representations (algebraic, graphical, computational) enhance understanding - R programming enables sophisticated analysis of real business problems - Interactive learning reinforces theoretical concepts with practical applications
Total estimated lecture time: 90 minutes Interactive elements: 6 quizzes + 6 R environments Assessment: 30 comprehensive questions across 5 levels of understanding